package com.liang.controller;


import com.alibaba.fastjson.JSON;
import com.liang.empty.ClientMsg;
import com.liang.empty.Result;
import com.liang.pojo.Userz;
import com.liang.security.MySecurity;
import com.liang.service.UserzService;
import com.liang.utils.Base64DecodeMultipartFile;
import com.liang.utils.IpUtil;
import com.liang.utils.tencentyun.TencentCOSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

;

/**
 *@作者:lsk
 *Controller接口
 *@Date:2023/10/11
*/
@RestController
@PropertySource(value = {"classpath:resource.properties","classpath:thirdparty.properties"})
@RequestMapping("/UserzURL")
public class UserzController {


	@Autowired
	private UserzService userzservice;


	@Autowired //操作键值都是字符串的
	private StringRedisTemplate redisTemplate;


	//在线
	@Value("${user.login}")
	private String UserLoginRedisKey;

	/**
	根据User_id查询
	*/

	@GetMapping("/selectByUser_id/{user_id}")
	public Result selectByUser_id(@PathVariable(value = "user_id") Long user_id){
		return Result.buildSuccess(userzservice.selectByUser_id(user_id));
	}

	/**
	 * 用户查询自己的信息
	 */
	@MySecurity
	@GetMapping("/selectByUser_idMyself/{user_id}")
	public Result selectByUser_idMyself(@PathVariable(value = "user_id") Long user_id){
		return Result.buildMessage(userzservice.selectByUser_id(user_id),"查询个人信息",200);
	}



	/**
	根据User_id更新
	*/
	@PostMapping("/updateByUser_id")
	public Result updateByUser_id(@RequestBody Userz demo){
		return Result.buildSuccess(userzservice.updateByUser_id( demo));
	}



	/**
	根据User_id删除
	*/
	@GetMapping("/deleteByUser_id/{user_id}")
	public Result deleteByUser_id(@PathVariable(value = "user_id") Long user_id){
		return Result.buildSuccess(userzservice.deleteByUser_id(user_id));
	}





	/**
	 insert:(插入)
	*/
	@PostMapping("/insert")
	public Result insert(@RequestBody Userz demo){
		Result.buildSuccess(userzservice.insert(demo));
		return Result.buildSuccess(demo);
	}


	//管理员使用
	/**
	 insertOrUpdate(插入或更新(根据主键或唯一索引更新，主键和唯一索引不会改变))
	*/
	@PostMapping("/insertOrUpdate")
	public Result insertOrUpdate(@RequestBody Userz demo){
		return Result.buildSuccess(userzservice.insertOrUpdate(demo));
	}



	/**
	 insertBath:(批量插入)
	*/
	@PostMapping("/insertBatch")
	public Result insertBatch(@RequestBody List<Userz> list){
		return Result.buildSuccess(userzservice.insertBatch(list));
	}



	/**
	 insertOrUpdateBatch:(批量插入或更新)
	*/
//	@PostMapping("/insertOrUpdateBatch")
	public Result insertOrUpdateBatch(@RequestBody List<Userz> list){
		return Result.buildSuccess(userzservice.insertOrUpdateBatch(list));
	}



	/**
	 selectList:(根据参数查询集合)
	*/
//	@PostMapping("/selectList")
	public Result selectList(@RequestBody Map<String,Object> param){
		return Result.buildSuccess(userzservice.selectList(param),userzservice.selectCount(param));
	}



	/**
	 selectCount:(根据集合查询数量)
	*/
//	@PostMapping("/selectCount")
	public Result selectCount(@RequestBody Map<String,Object> param){
		return Result.buildSuccess(userzservice.selectCount(param));
	}

	/**
	 * 查询用户所属军队,和职务和信息
	 */
	@PostMapping("/selectUserInforArmy")
	public Result selectUserInforArmy(@RequestBody Map<String,Object> param){
		Map map = new HashMap<String,Object>();
		if(param.get("pageNum") != null){
			map.put("total",userzservice.selectCount(param));
		}
		map.put("data",userzservice.selectUserInforArmy(param));
		return Result.buildMessage(map,"查询成功",200);
	}





	//用户密码登录
	@PostMapping("/user_password_login")
	public Result user_password_login (@RequestBody Map map,HttpServletRequest request) {
		Map param = new HashMap(); param.put("pageNum",1);param.put("pageSize",1);
		param.put("user_phone",map.get("user_phone"));
		Userz selectUser = null;
		try {
			selectUser  = (Userz) userzservice.selectList(param).get(0);
		}catch (Exception e){
			return Result.buildFail(666,"不存在该用户");
		}

		if(selectUser == null || selectUser.getStatus() != 0) {
			return Result.buildFail(666,"不存在该用户");
		}else {
			if(selectUser.getUser_password().equals(map.get("user_password").toString())){
				ClientMsg clientMsg = IpUtil.getRequestMsg(request);
				String Tokenx = UUID.randomUUID().toString();
				String UserAddress = IpUtil.getTokenPassword(clientMsg,Tokenx);
				selectUser.setUser_password(Tokenx);
				redisTemplate.opsForHash().put(UserLoginRedisKey,selectUser.getUser_id().toString(),UserAddress);
				return Result.buildSuccess(selectUser);
			}else{
				return Result.buildFail(666,"密码错误");
			}
		}
	}


	//用户上传图片
	@MySecurity
	@PostMapping("/UpdateWithHeader")
	public Result UpdateWithHeader(@RequestParam(value = "file") String base64Data,@RequestParam(value = "b_user_id") String b_user_id){

		MultipartFile file = Base64DecodeMultipartFile.base64Convert(base64Data);
		String userHeader = "";
		try {
			userHeader = TencentCOSUtil.uploadfile(file);
		}catch (Exception e){
			return Result.buildFail(666,"上传文件失败");
		}
		return Result.buildMessage(userHeader,"上传图像",200);


	}


}